
package com.company.calendar.config;

import com.company.calendar.entity.Company;
import com.company.calendar.entity.HolidayType;
import com.company.calendar.mapper.CompanyMapper;
import com.company.calendar.mapper.HolidayTypeMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * 数据初始化组件
 * 在应用启动时检查并初始化默认数据
 *
 * @author System
 * @since 1.0.0
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class DataInitializer implements CommandLineRunner {

    private final HolidayTypeMapper holidayTypeMapper;
    private final CompanyMapper companyMapper;

    @Override
    public void run(String... args) throws Exception {
        log.info("开始检查数据初始化...");
        initializeHolidayTypes();
        initializeCompanies();
        log.info("数据初始化检查完成");
    }

    /**
     * 初始化节假日类型数据
     */
    private void initializeHolidayTypes() {
        try {
            // 检查节假日类型表是否为空
            Long count = holidayTypeMapper.selectCount(null);
            if (count == null || count == 0) {
                log.info("节假日类型表为空，开始插入默认数据...");

                List<HolidayType> defaultHolidayTypes = Arrays.asList(
                    createHolidayType("法定节假日", "LEGAL_HOLIDAY", "#FF6B6B", "国家法定节假日"),
                    createHolidayType("公司节假日", "COMPANY_HOLIDAY", "#4ECDC4", "公司内部节假日"),
                    createHolidayType("调休工作日", "WORK_DAY", "#45B7D1", "调休工作日"),
                    createHolidayType("特殊工作日", "SPECIAL_WORK", "#96CEB4", "特殊工作日")
                );

                for (HolidayType holidayType : defaultHolidayTypes) {
                    holidayTypeMapper.insert(holidayType);
                }

                log.info("成功插入 {} 条节假日类型默认数据", defaultHolidayTypes.size());
            } else {
                log.info("节假日类型表已有 {} 条记录，跳过初始化", count);
            }
        } catch (Exception e) {
            log.error("初始化节假日类型数据失败: {}", e.getMessage(), e);
        }
    }

    /**
     * 初始化公司数据
     */
    private void initializeCompanies() {
        try {
            // 检查公司表是否为空
            Long count = companyMapper.selectCount(null);
            if (count == null || count == 0) {
                log.info("公司表为空，开始插入默认数据...");

                Company defaultCompany = new Company();
                defaultCompany.setCompanyName("默认公司");
                defaultCompany.setCompanyCode("DEFAULT_COMPANY");
                defaultCompany.setDescription("系统默认公司");

                companyMapper.insert(defaultCompany);

                log.info("成功插入默认公司数据，公司ID: {}", defaultCompany.getId());
            } else {
                log.info("公司表已有 {} 条记录，跳过初始化", count);
            }
        } catch (Exception e) {
            log.error("初始化公司数据失败: {}", e.getMessage(), e);
        }
    }

    /**
     * 创建节假日类型对象
     */
    private HolidayType createHolidayType(String typeName, String typeCode, String color, String description) {
        HolidayType holidayType = new HolidayType();
        holidayType.setTypeName(typeName);
        holidayType.setTypeCode(typeCode);
        holidayType.setColor(color);
        holidayType.setDescription(description);
        return holidayType;
    }
}
